package com.happyscan138.makefun.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.happyscan138.makefun.BaseAdapter.saokuText;
import com.happyscan138.makefun.R;

import java.util.List;

import static com.loopj.android.http.AsyncHttpClient.log;

/**
 * Created by xiaok on 2017/1/20.
 */

public class SaokuTextAdapter extends BaseAdapter {
    private Context mContext;
    public String positionText;
    public void setSaoOnAddClickListener(SaoKuAddClickListener onItemAddClick) {
        this.onItemAddClick = onItemAddClick;
    }

    public SaoKuAddClickListener onItemAddClick;

    public SaokuTextAdapter( ) {

    }

    public SaokuTextAdapter(Context mContext, List<saokuText> listData) {
        this.mContext = mContext;
        this.listData = listData;
    }

    private List<saokuText> listData;
    @Override
    public int getCount() {
        return listData.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        final ViewHolder_saoku_text viewHolder;
        //判断converView是否为空
        if(view==null){
            viewHolder=new ViewHolder_saoku_text();
            view=View.inflate(mContext, R.layout.saoku_text_item,null);
            viewHolder.saoku_text=(TextView) view.findViewById(R.id.saoku_text);
            //将viewHolder与converView绑定
            view.setTag(viewHolder);
        }else {
            //如果存在converView，取出保存在converView中的viewHolder
            viewHolder=(ViewHolder_saoku_text) view.getTag();
        }
        final saokuText bean=listData.get(position);
        //通过viewHolder找到对应控件，避免每次通过findViewById找控件
        viewHolder.saoku_text.setText(bean.text);
        positionText=bean.text;
        log.e("positionText",positionText);
        viewHolder.saoku_text.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(onItemAddClick != null) {
                    onItemAddClick.onItemClick(bean.text);
                }

            }
        });

        return view;
    }



    public  interface SaoKuAddClickListener {
        // true add; false cancel
        public void onItemClick(String positionText); //传递boolean类型数据给activity
    }
}

class ViewHolder_saoku_text{
    public TextView saoku_text;
}